﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharpcore7_ch14_ConcurrencyAndAsynchrony
{
    class Chapt14_3_4Continue
    {
        static void _Main()
        {
            Task<int> primeNumberTask = Task.Run(() =>
              Enumerable.Range(2, 3000000).Count(n =>
               Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));

            //var awaiter = primeNumberTask.GetAwaiter();
            var awaiter = primeNumberTask.ConfigureAwait(false).GetAwaiter();
            awaiter.OnCompleted(() =>
                {
                    int result = awaiter.GetResult();
                    Console.WriteLine(result);  //在控制台显示结果
                }
            );
            Console.Read();
        }


        static void __Main()
        {
            Task<int> primeNumberTask = Task.Run(() =>
              Enumerable.Range(2, 3000000).Count(n =>
               Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));
            primeNumberTask.ContinueWith(antecedent =>
            {
                int result = antecedent.Result;
                Console.WriteLine(result);
            }
            );
            Console.Read();
        }
    }
}
